package com.bawei.park2302.house

import androidx.activity.viewModels
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.HouseBean
import com.bawei.lib_base.utils.utils.entity.HouseBeanItem
import com.bawei.lib_base.utils.utils.entity.MyHouseBean
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.request.RequestType
import com.bawei.park2302.R
import com.bawei.park2302.adapter.HouseAreaAdapter
import com.bawei.park2302.databinding.ActivityHouseBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/HouseActivity")
@AndroidEntryPoint
class HouseActivity : BaseActivity<ActivityHouseBinding,HouseIntent,HouseViewModel>() {
    override val binding: ActivityHouseBinding by lazy {
        ActivityHouseBinding.inflate(layoutInflater)
    }
    override val vm: HouseViewModel by viewModels<HouseViewModel>()

    val houseAreaAdapter by lazy { HouseAreaAdapter(vm) }
    var data = 0
    override fun initView() {
        binding.rvHouse.apply {
            adapter = houseAreaAdapter
            layoutManager = LinearLayoutManager(this@HouseActivity)
        }

        //点击上面切换内容
        binding.houseRg.setOnCheckedChangeListener { radioGroup, checkId ->
            if (checkId == R.id.rb_a){
                vm.handelIntent(HouseIntent.houseRegionId(1))
            }else if (checkId == R.id.rb_b){
                vm.handelIntent(HouseIntent.houseRegionId(2))
            }else if (checkId == R.id.rb_c){
                vm.handelIntent(HouseIntent.houseRegionId(3))
            }else if (checkId == R.id.rb_d){
                vm.handelIntent(HouseIntent.houseRegionId(4))
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.APPROVE ->{
                                houseAreaAdapter.data.clear()
                                val beanItems = it.data as Collection<MyHouseBean>
                                val map = beanItems.groupBy { it.area  }
                                val list = map.keys.sorted()

                                val houseBean = mutableListOf<MyHouseBean>()
                                var index = 1
                                for (key in list){
                                    houseBean.add(MyHouseBean(index,key,map[key]!! as List<HouseBeanItem>))
                                    index++
                                }
                                houseAreaAdapter.addData(houseBean)
                                houseAreaAdapter.notifyDataSetChanged()
                            }

                            RequestType.RENT ->{
                                ToastUtils.showLong("出租成功")
                            }

                            RequestType.UN_RENT ->{
                                ToastUtils.showLong("退租成功")
                            }
                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.handelIntent(HouseIntent.house(data))
    }
}